package com.liy.cloud.base.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.liy.cloud.base.dao.BaseEquTypeParamDao;
import com.liy.cloud.base.dao.BaseTypeParamDao;
import com.liy.cloud.base.entity.BaseEquTypeParamEntity;
import com.liy.cloud.base.service.BaseEquTypeParamService;
import com.das.framework.common.dao.IBaseDao;
import com.das.framework.common.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * 关联设备的种类和参数信息 Service接口服务实现
 * @ClassName: BaseEquTypeParamServiceImpl
 * @Description: 
 * @author zhangw
 * @date: 2018-07-01 18:00:01
 */
@Service
public class BaseEquTypeParamServiceImpl extends BaseServiceImpl<BaseEquTypeParamEntity, String> implements BaseEquTypeParamService{


  @Autowired
  private BaseEquTypeParamDao baseEquTypeParamDao;

  @Autowired
  private BaseTypeParamDao baseTypeParamDao;

  @Override
  public IBaseDao<BaseEquTypeParamEntity, String> getDao() {
    return baseEquTypeParamDao;
  }

  @Override
  public void save(List<BaseEquTypeParamEntity> typeParamList, String typeId) throws Exception {

      //先删除原有关系，再插入新的关系
      baseEquTypeParamDao.deleteByTypeId(typeId);
      for(int i=0;i<typeParamList.size();i++){
        BaseEquTypeParamEntity baseEquTypeParam = JSON.parseObject(JSONObject.toJSONString(typeParamList.get(i),true),BaseEquTypeParamEntity.class);
        baseEquTypeParamDao.insertSelective(baseEquTypeParam);
      }
  }

  @Override
  public void update(List<BaseEquTypeParamEntity> typeParamList) throws Exception {

    for(BaseEquTypeParamEntity entity: typeParamList){
      baseEquTypeParamDao.updateByPrimaryKeySelective(entity);
    }
  }

  @Override
  public List<LinkedHashMap<String, Object>> getTypeParamInfo() throws Exception {
    List<LinkedHashMap<String, Object>> result = new LinkedList<>();
    // 查询所有的种类
    List<String> typeIdList = baseEquTypeParamDao.selectTypeIds();
    if(typeIdList != null){
      for (String typeId : typeIdList) {
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        map.put("typeId", typeId);
        map.put("paramList", baseTypeParamDao.selectByTypeId(typeId));
        result.add(map);
      }
    }
    return result;
  }

  @Override
  public List<String> selectTypeGroupname(String typeId) throws Exception {
    return baseEquTypeParamDao.selectTypeGroupname(typeId);
  }

  @Override
  public List<BaseEquTypeParamEntity> selectByTypeIdExport(BaseEquTypeParamEntity entity) {
    return baseEquTypeParamDao.selectByTypeIdExport(entity);
  }


}